//
//  LocalPushManager.m
//  bestkeep
//
//  Created by utouu666 on 17/1/25.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "LocalPushManager.h"
#import "Mediator+BKMy.h"
#import "Mediator+BKMain.h"

@implementation LocalPushManager

+(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    if (application.applicationState == UIApplicationStateActive) {
        return;
    }
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if ([self getCurrentViewControllerWithWindow:keyWindow]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(0.5 * NSEC_PER_SEC) ), dispatch_get_main_queue(), ^{
            
            if ([self islogin]) {
            UIViewController *integralVC = [[Mediator sharedInstance] BKIntegralViewController];
            [[self getCurrentViewControllerWithWindow:keyWindow] pushViewController:integralVC animated:YES];
                
            }else{
                Mediator *mediator = [Mediator sharedInstance];
                [mediator userGoToLoginFromOrignalController:[self getCurrentViewControllerWithWindow:keyWindow].visibleViewController finishBlock:^ {
                }];
 
                }
        });
    }
}
+(BOOL)islogin{
    Mediator *mediator = [Mediator sharedInstance];
    NSDictionary *dic = [mediator BKMyUserInfo];
    NSNumber *result = dic[BKMyUserIsLoginKey];
    if ([result intValue] == 0) {
        return NO;
    }
    return YES;
    
}


//获取当前控制器

+ (UINavigationController *) getCurrentViewControllerWithWindow:(UIWindow *)window
{
    UINavigationController *navigationController;
    
    UITabBarController *tabBarController = (UITabBarController *)window.rootViewController;
    
    if (tabBarController.selectedIndex == 0) {
        
        navigationController = window.rootViewController.childViewControllers.firstObject;
        
    }else if (tabBarController.selectedIndex == 1) {
        
        navigationController = window.rootViewController.childViewControllers[1];
        
    }else if (tabBarController.selectedIndex == 3) {
        
        navigationController = window.rootViewController.childViewControllers[3];
        
    }else {
        
        navigationController = window.rootViewController.childViewControllers.firstObject;
    }
    
    return navigationController;
    
}


@end
